var hotel = {
    events: {
        '.hotel-content-body .menu-operate-group1 click': 'addToCart',
        'body .shopping-item-row-sub click': 'goodMin',
        'body .shopping-item-row-add click': 'goodAdd',
    },
    dataList: [],
    init: function () {
        var _this = this;
        Util.event.call(_this, _this.events);
        $.each($(".hotel-menu-body .hotel-menu-item"),function (i,n) {
            (i+1)%3==0 ? $(n).css("margin-right","0") : "";
        });
        var _list = JSON.parse(localStorage.getItem("cart"));
        _this.dataList = Util.isEmpty(_list) ? [] : _list;
        _this.showCart();
    },
    addToCart: function (ele) {
        var _this = this;
        var shop_id = $(".my-main-content-title").data("shop-id"),shop_name = $(".div-to-main-page span").html();
        var $product_item = $(ele).parents(".hotel-menu-item"),product_id = $product_item.data("product-id"),product_name = $product_item.find(".menu-item-title").html();
        var bPFind = false,bFind = false,_json = {},_good_list = [],_good = {},pindex = 0,index = 0;
        $.each(_this.dataList,function (i,v) {
            if(v.shopid == shop_id){//同酒店
                _good_list = v.list;
                bPFind = true;
                pindex = i;
                $.each(_good_list,function (_i,_v) {
                    if(_v.productid == product_id){//同菜品
                        bFind = true;
                        index = _i;
                        return false;
                    }
                });
                return false;
            }
        });
        //加入菜单
        _good.productid = product_id;
        _good.name = product_name;
        _good.number = bFind ? _good_list[index].number+1 : 1;
        var price = $(ele).siblings(".menu-price").html();
        _good.price = price.substr(1,price.length-1);
        bFind ? (_good_list[index] = _good) : _good_list.push(_good);
        _json.shopid = shop_id;
        _json.shopname = shop_name;
        _json.list = _good_list;
        bPFind ? (_this.dataList[pindex] = _json) : _this.dataList.push(_json);
        _this.showCart();
        localStorage.setItem("cart",JSON.stringify(_this.dataList));
    },
    goodAdd :function (ele) {
        var $_text = $(ele).siblings(".shopping-item-row-text"),pindex = $(ele).parents(".shopping-item").data("pindex"),index = $(ele).parents(".shopping-item-row").data("index");
        var count = parseInt($_text.html());
        count++;
        this.dataList[pindex].list[index].number = count;
        this.showCart();
        localStorage.setItem("cart",JSON.stringify(_this.dataList));
    },
    goodMin: function (ele) {
        var $_text = $(ele).siblings(".shopping-item-row-text"),pindex = $(ele).parents(".shopping-item").data("pindex"),index = $(ele).parents(".shopping-item-row").data("index");
        var count = parseInt($_text.html());
        if(count <= 1){
            if(this.dataList[pindex].list.length == 1){
                this.dataList.splice(pindex);
            }else{
                this.dataList[pindex].list.splice(index,1);
            }
        }else{
            count--;
            this.dataList[pindex].list[index].number = count;
        }
        this.showCart();
        localStorage.setItem("cart",JSON.stringify(_this.dataList));
    },
    showCart: function () {
        var _this = this;
        var temp = "",total = 0;
        $.each(_this.dataList,function (i,v) {
            //酒店
            var shopid = v.shopid,shopname = v.shopname;
            temp += "<div class='shopping-item' data-pindex='"+i+"'><div class='shopping-item-header' data-shop-id='" + shopid + "'>" + shopname + "</div><div class='shopping-item-body'>";
            //菜品
            var _list_str = "";
            $.each(v.list,function (index,item) {
                _list_str += "<div class='shopping-item-row' data-product-id='" + item.productid
                    + "' data-index='"+index+"'><div class='shopping-item-row-name'>" + item.name +"</div><div class='shopping-item-row-operate'><div class='shopping-item-row-sub'>-</div>"
                    + "<div class='shopping-item-row-text'>" + item.number + "</div><div class='shopping-item-row-add'>+</div></div>"
                    + "<div class='shopping-item-row-price'>¥" + item.price + "</div></div>";
                total += item.number * item.price;
            });
            temp += _list_str + "</div></div>";
        });
        $(".shopping-trolley-body").html(Util.isEmpty(temp)?"<div class='cart-empty'>您的购物车是空的！</div>":temp);
        $(".shopping-trolley-total-text").html("共"+total+"元")
    }
};